package com.cn.mobileproject.db.dao.impl;

import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.cn.mobileproject.bean.Org;
import com.cn.mobileproject.bean.User;
import com.cn.mobileproject.db.Query;
import com.cn.mobileproject.db.RowMapper;
import com.cn.mobileproject.db.SqliteTemplate;
import com.cn.mobileproject.db.UDatabase;
import com.cn.mobileproject.db.dao.UserDao;
import com.cn.mobileproject.db.table.OrgTable;
import com.cn.mobileproject.db.table.UserTable;

public class UserDaoImpl implements UserDao {

	private SqliteTemplate sqliteTemplate;

	public UserDaoImpl() {
		sqliteTemplate = new SqliteTemplate();
	}

	@Override
	public void insert(User user) {
		if(user==null||user.username==null||user.username.equals("")) return;
		if (!isExists(user)) {
			Query query = new Query();
			query.into(UserTable.TABLE_NAME).values(
					userToValues(user));
			sqliteTemplate.insert(query);
		} else {
			update(user);
		}
	}

	@Override
	public void delete(User user) {
		Query query = new Query();
		query.from(UserTable.TABLE_NAME).where(UserTable.FIELD_UID + "=?", user.uid);
		sqliteTemplate.delete(query);
	}

	@Override
	public void update(User user) {
		Query query = new Query();
		query.setTable(UserTable.TABLE_NAME).where(UserTable.FIELD_UID + "=?", user.uid).values(userToValues(user));
		sqliteTemplate.upload(query) ;
	}

	public boolean isExists(User user) {
		boolean result = false;
		Query query = new Query(UDatabase.getDb(false));
		query.from(UserTable.TABLE_NAME, null)
			.where(UserTable.FIELD_UID + " = ?",user.uid);
		Cursor cursor = query.select();
		result = (cursor != null && cursor.getCount() > 0);
		cursor.close();
		return result;
	}

	private ContentValues userToValues(User user) {
		ContentValues v = new ContentValues();
		v.put(UserTable.FIELD_ACCOUNT, user.account);
		v.put(UserTable.FIELD_ORGID, user.orgId);
		v.put(UserTable.FIELD_ORGNAME, user.orgName);
		v.put(UserTable.FIELD_PASSWORD, user.password);
		v.put(UserTable.FIELD_USERNAME, user.username);
		v.put(UserTable.FIELD_UID, user.uid);
		v.put(UserTable.FIELD_TIME, System.currentTimeMillis());
		return v;
	}

	private static final class UserdMapper implements
			RowMapper<User> {
		public User mapRow(Cursor cursor, int rowNum) {
			User user = new User();
			if (cursor != null && cursor.getCount() > 0) {
				user.account=cursor.getString(cursor.getColumnIndex(UserTable.FIELD_ACCOUNT));
				user.orgId=cursor.getString(cursor.getColumnIndex(UserTable.FIELD_ORGID));
				user.orgName=cursor.getString(cursor.getColumnIndex(UserTable.FIELD_ORGNAME));
				user.password=cursor.getString(cursor.getColumnIndex(UserTable.FIELD_PASSWORD));
				user.uid=cursor.getString(cursor.getColumnIndex(UserTable.FIELD_UID));
				user.username=cursor.getString(cursor.getColumnIndex(UserTable.FIELD_USERNAME));
			}
			return user;
		}
	}

	@Override
	public User searchLastLoginUser() {
		String limitpar = 0 + "," + 5;
		Query query = new Query();
		query.from(UserTable.TABLE_NAME, null).orderBy(UserTable.FIELD_TIME + " DESC ").limit(limitpar);
		List<User> list = sqliteTemplate.queryForList(query,new UserdMapper());
		if (list != null && list.size() != 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<User> searchUsers() {
		Query query = new Query();
		query.from(UserTable.TABLE_NAME, null).orderBy(UserTable.FIELD_ACCOUNT + " ASC ");
		List<User> list = sqliteTemplate.queryForList(query,new UserdMapper());
		return list;
	}

	@Override
	public void inserts(List<User> list) {
		if(list==null||list.size()==0) return ;
		try{
			SQLiteDatabase db = UDatabase.getDb(true);
			db.beginTransaction();
			for(User user: list){
				db.replace(UserTable.TABLE_NAME, null, userToValues(user));
			}
			db.setTransactionSuccessful();
			db.endTransaction();
		}catch(Exception e){
		}
		
	}

}
